package com.desmart.renjiao.main.home;

import android.util.Log;

import com.desmart.renjiao.xmlparase.EmailXmlUtils;
import com.desmart.renjiao.xmlparase.ResultXmlUtils;
import com.lemon.library.net.rx.RxObservableListener;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class HomeP extends Home.HomeP{

    @Override
    void getEmail(String username) {

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<urn:unread>\n" +
                "<username>" + username +
                "</username>\n" +
                "</urn:unread>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>";


        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.getEmail(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                String ss = null;

                try {
                    ss = responseBody.string();
                    Log.e("Hqq",ss);
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = EmailXmlUtils.readXML(inputStream);
                    mView.onGetEmailUnRead(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }


}
